/*
 * Copyright (c) 2014, 闂堟帒鐭濋崣鎼侊拷姘鳖潠閹讹拷閺堝妾洪崗顒�寰�? All rights reserved.
 * File Name閿涙瓈oadingDialog.java
 * Version閿涙�?1.0
 * Author閿涙haokaiqiang
 * Date閿涳�?2014-10-24
 */

package com.greenant.store.view;

import java.util.Timer;
import java.util.TimerTask;

import com.greenant.store.R;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

/**
 * 
 * @ClassName: com.example.animationloading.LoadingDialog
 * @Description: 閸斻劎鏁鹃崝鐘烘祰Dialog
 * @author zhaokaiqiang
 * @date 2014-10-27 娑撳宕�?4:42:52
 * 
 */
public class LoadingDialog extends Dialog {

	protected static final String TAG = "LoadingDialog";
	// 閸斻劎鏁鹃梻鎾�?
	private static final int DURATION = 800;
	// 閸撳秵娅欓崶鍓у
	private ImageView img_front;
	// 鐎规碍妞傞崳顭掔礉閻€劍娼垫稉宥嗘焽閻ㄥ嫭鎸遍弨鎯уЗ閻拷
	private Timer animationTimer;
	// 閺冨娴嗛崝銊ф暰
	private RotateAnimation animationL2R;

	private Animation animation;

	private Context context;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			img_front.setAnimation(animationL2R);
			animationL2R.start();
			// img_front.setAnimation(animation);
			// animation.start();
		};

	};

	public LoadingDialog(Context context) {
		super(context, R.style.dialog);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_loading);

		img_front = (ImageView) findViewById(R.id.img_front);
		animationTimer = new Timer();

		// 娴犲骸涔忛崚鏉垮礁閻ㄥ嫭妫嗘潪顒�濮╅悽浼欑礉鐠佸墽鐤嗛弮�?�祮鐟欐帒瀹抽崪灞炬鏉烆兛鑵戣箛锟�
		animationL2R = new RotateAnimation(0f, 360f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		// 鐠佸墽鐤嗛崝銊ф暰閻ㄥ嫯绻嶇悰灞炬闂�锟�?
		animationL2R.setDuration(DURATION);
		// 閸斻劎鏁炬潻鎰攽缂佹挻娼稊�?�倵閿涘奔绻氱�涙绮ㄩ弶鐔剁閸氬海娈戦悩鑸碉拷锟�
		animationL2R.setFillAfter(true);
		// 鐠佸墽鐤嗛柌宥咁槻閻ㄥ嫭顐奸弫锟�?
		animationL2R.setRepeatCount(1);
		// 鐠佸墽鐤嗛柌宥咁槻濡�崇础娑撴椽锟藉棜绻嶉崝锟�?
		//animationL2R.setRepeatMode(Animation.REVERSE);

		// animation = AnimationUtils.loadAnimation(context,
		// R.anim.anim_load_dialog);

		// 閹笛嗩攽闂傛挳娈ф禒璇插閿涘苯绱戞慨瀣？闂呮梹妲�0閿涘本鐦￠梾鎿燯RATION * 2閹笛嗩攽娑擄拷濞嗭拷
		animationTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				handler.sendEmptyMessage(1);
			}
		}, 0, DURATION * 2);

	}

	@Override
	protected void onStop() {
		super.onStop();
		animationTimer.cancel();
	}

}